package com.orangelife.service.propFeeReceiptLink.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeReceiptLink.IPropFeeReceiptLinkService;
import com.orangelife.service.propFeeReceiptLink.model.PropFeeReceiptLinkDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeReceiptLink类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-12-19 11:13:55
 * ==============================================================
 */
@Service("propFeeReceiptLinkService")
public class PropFeeReceiptLinkServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeReceiptLinkService {
	private static Logger logger = Logger
			.getLogger(PropFeeReceiptLinkServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeReceiptLinkServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeReceiptLink(PropFeeReceiptLinkDto propFeeReceiptLink)
			throws AppBizException {

		Assert.notNull(propFeeReceiptLink);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeReceiptLink.getType())) {
			if (propFeeReceiptLink.getType().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "类型长度超出");
			}
		}

		propFeeReceiptLink.setReceiptLinkID(IdUtil.getuuId());
		propFeeReceiptLink.setItemStat(TypeConst.FLAG_ON);
		propFeeReceiptLink.setTscreate(new Date());

		return this.getSqlSession().insert(
				"PropFeeReceiptLinkMapper.insertSelective", propFeeReceiptLink);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeReceiptLink(PropFeeReceiptLinkDto propFeeReceiptLink)
			throws AppBizException {

		Assert.notNull(propFeeReceiptLink);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeReceiptLink.getReceiptLinkID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeReceiptLinkDto oldPropFeeReceiptLink = this
				.findPropFeeReceiptLinkByID(propFeeReceiptLink
						.getReceiptLinkID());

		if (!StringUtils.isBlank(propFeeReceiptLink.getType())) {
			if (propFeeReceiptLink.getType().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "类型长度超出");
			}
		}

		if (oldPropFeeReceiptLink == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		return this.getSqlSession().update(
				"PropFeeReceiptLinkMapper.updateByPrimaryKeySelective",
				propFeeReceiptLink);
	}

	@Override
	public PropFeeReceiptLinkDto findPropFeeReceiptLinkByID(
			String propFeeReceiptLinkID) throws AppBizException {
		if (StringUtils.isBlank(propFeeReceiptLinkID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeReceiptLinkDto oldPropFeeReceiptLink = this.getSqlSession()
				.selectOne("PropFeeReceiptLinkSearchMapper.selectByPrimaryKey",
						propFeeReceiptLinkID);
		if (oldPropFeeReceiptLink == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeReceiptLink.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeReceiptLink;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeReceiptLinkByLogic(String propFeeReceiptLinkID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeReceiptLinkID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeReceiptLinkDto oldPropFeeReceiptLink = this
				.findPropFeeReceiptLinkByID(propFeeReceiptLinkID);

		oldPropFeeReceiptLink.setItemStat(TypeConst.FLAG_OFF);

		return this.getSqlSession().update(
				"PropFeeReceiptLinkMapper.updateByPrimaryKeySelective",
				oldPropFeeReceiptLink);
	}

	@Override
	public List<Map<String, Object>> findPropFeeReceiptLinkByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReceiptLinkSearchMapper.findAllPropFeeReceiptLink",
						map);
		return list;
	}

	@Override
	public List<PropFeeReceiptLinkDto> getPropFeeReceiptLinkByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeReceiptLinkDto> list = this.getSqlSession().selectList(
				"PropFeeReceiptLinkSearchMapper.getAllPropFeeReceiptLinkDto",
				map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeReceiptLinkByRelevantID(
			String receiptLinkID, String receiptID, String type, String linkID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("receiptLinkID", receiptLinkID);
		map.put("receiptID", receiptID);
		map.put("type", type);
		map.put("linkID", linkID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReceiptLinkSearchMapper.selectPropFeeReceiptLinkDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeReceiptLinkDto> findPropFeeReceiptLinkByRelevantID(
			String receiptLinkID, String receiptID, String type, String linkID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("receiptLinkID", receiptLinkID);
		map.put("receiptID", receiptID);
		map.put("type", type);
		map.put("linkID", linkID);

		List<PropFeeReceiptLinkDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReceiptLinkSearchMapper.findPropFeeReceiptLinkDtoByRelevantID",
						map);
		return list;
	}

	@Override
	@Cacheable(value = "bauthPropFeeReceiptLink")
	public List<PropFeeReceiptLinkDto> bauthfindPropFeeReceiptLink(
			List<String> propFeeReceiptLinkIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeReceiptLinkDto> list = this.getSqlSession().selectList(
				"PropFeeReceiptLinkSearchMapper.batchFindPropFeeReceiptLink",
				propFeeReceiptLinkIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeReceiptLinkByMap(
			Map<String, Object> propFeeReceiptLink) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReceiptLinkSearchMapper.batchFindPropFeeReceiptLinkByMap",
						propFeeReceiptLink);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	public int addPropFeeReceiptLink(
			List<PropFeeReceiptLinkDto> propFeeReceiptLink)
			throws AppBizException {

		if (propFeeReceiptLink != null && propFeeReceiptLink.size() > 0) {
			return this.getSqlSession().insert(
					"PropFeeReceiptLinkBauthMapper.bauthInsert",
					propFeeReceiptLink);
		}
		return 0;
	}

}